$("#goods").bootstrapTable({
    url: "/goods/list",
    method: "get",
    pagination: true,   //是否显示分页（*）
    pageSize: 10,       //每页的记录行数（*）
    pageList: [10, 20, 50, 100, 'All'], //可供选择的每页的行数（*）
    sidePagination: "client",    //分页方式：client客户端分页，server服务端分页（*）
    pageNumber: 1,                    //初始化加载第一页，默认第一页
    classes: "table table-bordered table-striped table-condensed table-hover",
    // 动态修改行的背景颜色
    rowStyle: function (row, index) { //row：行对象， index ：第几行
        // if (row.warnBoxNum == null || row.warnBoxNum == 0) {
        //     // 预警库存为0，不标记
        //     return {css: {"background-color": 'rgb(255,255,255)'}};
        // } else
            if (row.boxNum < 0) {
            //库存小于0，即已经缺货
            return {css: {"background-color": 'rgb(218,112,214)'}};
        } else if (row.boxNum < row.warnBoxNum) {
            //预警库存不为0，且库存低于预警库存
            return {css: {"background-color": 'rgb(255,125,64)'}};
        } else {
            return {css: {"background-color": 'rgb(255,255,255)'}};
        }
    },
    queryParams:function (params){
        goodsName: $("#search-goodsName").val();
    },
    columns: [{
        title: "商品id",
        field: "id",
            visible:false
    }, {
        title: "商品名称",
        field: "goodsName",
    }, {
        title: "库存数量（盒/箱）",
        field: "boxNum",
    }, {
        title: "前30天销量（盒）",
        field: "warnBoxNum",
    }, {
        title: "入库成本：元",
        field: "inPriceYuan",
    }, {
        title: "商品简称",
        field: "simpleGoodsName",
    }, {
        title: "散装库存",
        field: "num",
    }, {
        title: "规格（个/盒）",
        field: "perBox",
    }, {
        title: "备注",
        field: "note",
    }, {
        title: "重量：g",
        field: "weight",
    }, {
        field: 'operate',
        title: '操作',
        align: 'center',
        valign: 'middle',
        width: 200,
        events: {
            'click #update': function (e, value, row, index) {
                $('#update-id').val(row.id);
                $('#update-goodsName').val(row.goodsName);
                $('#update-simpleGoodsName').val(row.simpleGoodsName);
                $('#update-inPriceYuan').val(row.inPriceYuan);
                $('#update-perBox').val(row.perBox);
                $('#update-num').val(row.num);
                $('#update-boxNum').val(row.boxNum);
                $('#update-note').val(row.note);
                $('#update-weight').val(row.weight);
            },
            'click #delete': function (e, value, row, index) {
                $('#delete-id').val(row.id);
                $('#delete-goodsName').val(row.goodsName);
                $('#delete-num').val(row.num);
                $('#delete-perBox').val(row.perBox);
                $('#delete-boxNum').val(row.boxNum);
                $('#delete-note').val(row.note);
            }
        },
        formatter: function (value, row, index) {
            var result = "";
            result += '<button id="update" class="btn btn-info" data-toggle="modal" data-target="#updateModal">编辑</button>';
            result += '<button id="delete" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal" style="margin-left:10px;">删除</button>';
            return result;
        }

    }]
});

function searchGoodsOnClick() {
    $.get({
        url: "/goods/list",
        data: {
            goodsName: $("#search-goodsName").val()
        },
        dataType: "json",
        success: function (json) {
            $("#goods").bootstrapTable('load', json);
        }
    });
}

//按下Enter查询
$(document).keyup(function (event) {
    if (event.keyCode == 13){
        searchGoodsOnClick();
    }

});
function exportOnClick() {
    $.get({
        url: "/goods/export",
        data: {
            goodsName: $("#search-goodsName").val()
        },
        dataType: "json",
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，导出商品数量:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
}


$("#reset").click(function () {
    $("#search-goodsName").val("");
})

$("#addGoods").click(function () {
    $.post({
        url: "/goods/addOne",
        async: false,//同步，默认是true，异步
        data: {
            goodsName: $("#add-goodsName").val(),
            simpleGoodsName: $("#add-simpleGoodsName").val(),
            perBox: $("#add-perBox").val(),
            inPriceCent: $("#add-inPriceYuan").val() * 100
        },
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
    $("#addModal").modal('hide');
    searchGoodsOnClick();
});

$("#updateGoods").click(function () {
    $.post({
        url: "/goods/update",
        async: false,//同步，默认是true，异步
        data: {
            id: $("#update-id").val(),
            goodsName: $("#update-goodsName").val(),
            simpleGoodsName: $("#update-simpleGoodsName").val(),
            inPriceCent: $("#update-inPriceYuan").val() * 100,
            perBox: $("#update-perBox").val(),
            num: $("#update-num").val(),
            boxNum: $("#update-boxNum").val(),
            note: $("#update-note").val(),
            weight: $("#update-weight").val(),
        },
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
    $("#updateModal").modal('hide');
    searchGoodsOnClick();
});

$("#deleteGoods").click(function () {
    $.post({
        url: "/goods/delete",
        async: false,//同步，默认是true，异步
        data: {
            id: $("#delete-id").val()
        },
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
    $("#deleteModal").modal('hide');
    searchGoodsOnClick();
});

$("#update-perBox").on("input", function () {
    let perNum = $("#update-perBox").val() == null ? 1 : $("#update-perBox").val();
    $("#update-boxNum").val($("#update-num").val() / perNum);
});

$("#update-num").on("input", function () {
    let perNum = $("#update-perBox").val() == null ? 1 : $("#update-perBox").val();
    $("#update-boxNum").val($("#update-num").val() / perNum);
});

$("#update-boxNum").on("input", function () {
    let perNum = $("#update-perBox").val() == null ? 1 : $("#update-perBox").val();
    $("#update-num").val($("#update-boxNum").val() * perNum);
});
